/* @(#)FseDocumentSectionView.java
** 
** Copyright (C) 2012 by Steven D. Stamps
**
**             Trademarks & Copyrights
** Flywheel Management Science(TM), Flywheel Management Model(TM),
** Flywheel Story Editor(TM) and FlywheelMS(TM) are exclusive trademarks
** of Steven D. Stamps and may only be used freely for the purpose of
** identifying the unforked version of this software.  Subsequent forks
** may not use these trademarks.  All other rights are reserved.
**
** DecKanGL (Decorated Kanban Glyph Language) and TribKn (Tribal Knowledge)
** are also exclusive trademarks of Steven D. Stamps.  These may be used
** freely within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** gConGUI (game Controller Graphical User Interface) is an exclusive
** trademark of Steven D. Stamps.  This trademark may be used freely
** within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** Trademark information is available at
** <http://www.flywheelms.com/trademarks>
**
** Flywheel Management Science(TM) is a copyrighted body of management
** metaphors, governance processes, and leadership techniques that is
** owned by Steven D. Stamps.  These copyrighted materials may be freely
** used, without alteration, by the community (users and developers)
** surrounding this GPL3-licensed software.  Additional copyright
** information is available at <http://www.flywheelms.org/copyrights>
**
**              GPL3 Software License
** This program is free software: you can use it, redistribute it and/or
** modify it under the terms of the GNU General Public License, version 3,
** as published by the Free Software Foundation. This program is distributed
** in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
** even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
** PURPOSE.  See the GNU General Public License for more details. You should
** have received a copy of the GNU General Public License, in a file named
** COPYING, along with this program.  If you cannot find your copy, see
** <http://www.gnu.org/licenses/gpl-3.0.html>.
*/

package com.flywheelms.library.fse.views;

import java.util.ArrayList;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.flywheelms.library.FmsLibraryActivity;
import com.flywheelms.library.R;
import com.flywheelms.library.fdk.enumerator.FdkState;
import com.flywheelms.library.fdk.interfaces.FdkClient;
import com.flywheelms.library.fse.FseDocumentSectionType;
import com.flywheelms.library.fse.fragment.FlywheelStoryEditorFragment;
import com.flywheelms.library.fse.model.FseDocumentSection;
import com.flywheelms.library.gcg.helper.GcgHelper;
import com.flywheelms.library.gcg.interfaces.GcgFlingTarget;

public abstract class FseDocumentSectionViewLegacy extends LinearLayout
		implements GcgFlingTarget, FdkClient {

	protected FseDocumentView documentView;
	// will be assigned by concrete implementation
	protected FseDocumentSectionType sectionType;
	protected FdkState defaultFdkState = FdkState.DISABLED;
	protected FdkState localFdkState = this.defaultFdkState;
	protected boolean areFdkKeypadsActive = false;
	protected Button tabControlTribKnButton;
	protected Button tabControlAuditButton;
	protected Button tabControlHistoryButton;
	protected Button tabControlNotesButton;
	protected Button tabControlStoryButton;

	public FseDocumentSectionViewLegacy(Context aContext, AttributeSet anAttributeSet) {
		super(aContext, anAttributeSet);
		setDefaultFlywheelDictationKeyboardState();
	}
	
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		return true;
	}
	
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return false;
    }
	
	public void initialize(FseDocumentView aDocumentView) {
		this.documentView = aDocumentView;
	}
	
	protected FlywheelStoryEditorFragment getFseFragment() {
		return this.documentView.getFseFragment();
	}

	public FseDocumentView getDocumentView() {
		return this.documentView;
	}
	
	public abstract String getHelpContextUrlString();
	
	public void setHelpContextUrlString() {
		((FmsLibraryActivity) this.documentView.getFseFragment().getActivity())
			.setHelpContextUrlString(getHelpContextUrlString());
	}

	public int getDocumentSectionViewResourceId() {
		return this.sectionType.getViewId();
	}
	
	public abstract ViewGroup getDocumentSectionLayout();

	public abstract void setDocumentSectionLayout();

	public FseDocumentSectionType getSectionType() {
		return this.sectionType;
	}

	protected void setSectionType(FseDocumentSectionType aDocumentSectionType) {
		this.sectionType = aDocumentSectionType;
	}
	
	abstract void viewDocumentSection(FseDocumentSection aDocumentSection);
	
	protected void activateDocumentSectionTab() {
		setHelpContextUrlString();
		restoreFlywheelDictationKeyboardState();
	}

	protected void deactivateDocumentSectionTab() {
		fdkCancelDictation();
	}
	
	abstract String sectionIsModified();

	protected void initTabControlButtons() {
		EditText.OnKeyListener onKeyListener = new EditText.OnKeyListener() {
			
		    @Override
			public boolean onKey(View v, int aKeyCode, KeyEvent aKeyEvent) {
		    	if(aKeyEvent.getAction() == KeyEvent.ACTION_DOWN) {
		    		if (aKeyCode == KeyEvent.KEYCODE_RIGHT_BRACKET) {
		    			if(aKeyEvent.isCtrlPressed()) {
		    				clickNextTabControlButton();
		    				return true;
		    			}
		    		}
		    		if (aKeyCode == KeyEvent.KEYCODE_LEFT_BRACKET) {
		    			if(aKeyEvent.isCtrlPressed()) {
		    				clickPreviousTabControlButton();
		    				return true;
		    			}
		    		}
		    	}
	            return false;
	        }
	    };
		this.tabControlTribKnButton = (Button) getDocumentSectionLayout().findViewById(R.id.fse__document_section__tribkn);
		this.tabControlTribKnButton.setOnKeyListener(onKeyListener);
		this.tabControlTribKnButton.setFocusable(true);
		this.tabControlTribKnButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				FseDocumentSectionViewLegacy.this.getDocumentView().activateDocumentSectionTab(
						FseDocumentSectionType.TRIBKN);
			}
		});
		this.tabControlAuditButton = (Button) getDocumentSectionLayout().findViewById(R.id.fse__document_section__audit_block__button);
		this.tabControlAuditButton.setOnKeyListener(onKeyListener);
		this.tabControlAuditButton.setFocusable(true);
		this.tabControlAuditButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				FseDocumentSectionViewLegacy.this.getDocumentView().activateDocumentSectionTab(
						FseDocumentSectionType.AUDIT);
			}
		});
		this.tabControlHistoryButton = (Button) getDocumentSectionLayout().findViewById(R.id.fse__document_section__history__button);
		this.tabControlHistoryButton.setOnKeyListener(onKeyListener);
		this.tabControlHistoryButton.setFocusable(true);
		this.tabControlHistoryButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				FseDocumentSectionViewLegacy.this.getDocumentView().activateDocumentSectionTab(
						FseDocumentSectionType.HISTORY);
			}
		});
		this.tabControlStoryButton = (Button) this.getDocumentSectionLayout().findViewById(R.id.fse__document_section__story__button);
		this.tabControlStoryButton.setOnKeyListener(onKeyListener);
		this.tabControlStoryButton.setFocusable(true);
		this.tabControlStoryButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				FseDocumentSectionViewLegacy.this.getDocumentView().activateDocumentSectionTab(
						FseDocumentSectionType.STORY);
			}
		});
		this.tabControlNotesButton = (Button) getDocumentSectionLayout().findViewById(R.id.fse__document_section__notes__button);
		this.tabControlNotesButton.setOnKeyListener(onKeyListener);
		this.tabControlNotesButton.setFocusable(true);
		this.tabControlNotesButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				FseDocumentSectionViewLegacy.this.getDocumentView().activateDocumentSectionTab(
						FseDocumentSectionType.NOTES);
			}
		});
		disableActiveTabControlButton();
	}
	
	@SuppressWarnings("null")
	private void disableActiveTabControlButton() {
		Button theButton = null;
		switch (this.sectionType) {
			case TRIBKN:
				theButton = this.tabControlTribKnButton;
				break;
			case AUDIT:
				theButton = this.tabControlAuditButton;
				break;
			case HISTORY:
				theButton = this.tabControlHistoryButton;
				break;
			case STORY:
				theButton = this.tabControlStoryButton;
				break;
			case NOTES:
				theButton = this.tabControlNotesButton;
				break;
			default:
				// programming error - we want this to blow up  :-)
				break;
		}
		theButton.setBackgroundResource(R.color.BlueViolet);
		theButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// in the future we may want this to do something special, like section history or etc.
				return;
			}
		});
	}
	
	public abstract void clickNextTabControlButton();
	
	public abstract void clickPreviousTabControlButton();

	@Override
	public void onFlingUp() {
		return;
	}

	@Override
	public void onFlingDown() {
		return;
	}

	@Override
	public void onFlingRight() {
		clickPreviousTabControlButton();
	}

	@Override
	public void onFlingLeft() {
		clickNextTabControlButton();
	}
	
	/////////////////////////////////////////


	protected void setDefaultFlywheelDictationKeyboardState() {
		this.defaultFdkState = FdkState.DISABLED;
		this.localFdkState = this.defaultFdkState;
	}
	
	public void restoreFlywheelDictationKeyboardState() {
		if(GcgHelper.isLandscapeMode()) {
			((FmsLibraryActivity) this.documentView.getFseFragment().getActivity())
			.setFlywheelDictationKeyboardState(this.localFdkState);
			if(this.localFdkState != FdkState.DISABLED) {
				((FmsLibraryActivity) this.documentView.getFseFragment().getActivity())
				.registerFdkListener(this);
				fdkActivateKeypads(this.areFdkKeypadsActive);
			}
		}
	}

	public FdkState getDefaultFlywheelDictationKeyboardState() {
		return this.defaultFdkState;
	}

	public FdkState getFlywheelDictationKeyboardState() {
		return ((FmsLibraryActivity) this.documentView.getFseFragment().getActivity())
				.getFlywheelDictationKeyboardState();
	}
	
	protected LinearLayout fdkGetRightKeypad() {
		return null;
	}
	
	protected LinearLayout fdkGetLeftKeypad() {
		return null;
	}
	
	@Override
	public void fdkActivateKeypads(boolean aBoolean) {
		return;
	}
	
	@Override
	public void fdkStartDictation() {
		return;
	}
	
	@Override
	public void fdkStopDictation() {
		return;
	}
	
	@Override
	public void fdkCancelDictation() {
		return;
	}
	
	@Override
	public void fdkNotifyStateChangeByActivity(FdkState anFdkState) {
		if(this.documentView.getActiveDocumentSectionView() != this) {
			return;
		}
		this.localFdkState = anFdkState;
		switch(anFdkState) {
			case ACTIVE:
				fdkActivateKeypads(true);
				break;
			case AVAILABLE:
				fdkActivateKeypads(false);
				break;
			default:
		}
	}
	
	@Override
	public void fdkDictationResults(ArrayList<String> theWordList) {
		return;
	}

}
